<template>
  <el-card shadow="hover">
    <!--条件查询导航 -->
<!--    {{studentSearch}}-->
    <el-form :inline="true" :model="studentSearch" class="demo-form-inline">
      <el-form-item label="学生姓名">
        <el-input v-model="studentSearch.sname" placeholder="请输入学生姓名"></el-input>
      </el-form-item>
      <el-form-item label="学生姓名">
        <el-input v-model="studentSearch.addr" placeholder="请输入学生住址"></el-input>
      </el-form-item>
      <el-form-item label="所在班级">
        <el-select v-model="studentSearch.cid" placeholder="请选择班级">
          <el-option label="所有班级" value="0"></el-option>
          <el-option :label="c.cname" :value="c.cid" v-for="c in classes" :key="c.cid"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" @click="search" plain>查询</el-button>
        <el-button type="success" @click="addUI" plain>添加</el-button>
        <el-button type="danger" @click="handleDelete" plain>批量删除</el-button>
      </el-form-item>
    </el-form>
    <el-table
      :data="students"
      style="width: 100%"
      @selection-change="handleSelectionChange">
      <el-table-column
        type="selection"
        align="center"
        width="55">
      </el-table-column>
      <el-table-column
        label="姓名"
        prop="sname"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="性别"
        prop="sex"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="年龄"
        prop="age"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="住址"
        prop="addr"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column
        label="所在班级"
        prop="cname"
        align="center"
        width="180">
      </el-table-column>
      <el-table-column label="操作" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            @click="handleEdit(scope.$index, scope.row)">编辑</el-button>
          <el-button
            size="mini"
            type="danger"
            @click="handleDelete(scope.$index, scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!--1. 分页导航 -->
    <el-pagination
      @size-change="handleSizeChange"
      @current-change="handleCurrentChange"
      :current-page="page"
      :page-sizes="[5, 10, 15, 20]"
      :page-size="pageSize"
      layout="total, sizes, prev, pager, next, jumper"
      align="right"
      :total="total">
    </el-pagination>
    <!--2. 添加或修改对话框 -->
    <el-dialog :title="studentForm.sid ? '修改学生': '添加学生'" :visible.sync="dialogFormVisible">
      {{studentForm}}
      <el-form :model="studentForm">
        <el-form-item label="学生姓名" :label-width="formLabelWidth">
          <el-input v-model="studentForm.sname" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生性别" :label-width="formLabelWidth">
          <el-radio-group v-model="studentForm.sex">
            <el-radio label="男" value="男"/>
            <el-radio label="女" value="女"/>
          </el-radio-group>
        </el-form-item>
        <el-form-item label="学生年龄" :label-width="formLabelWidth">
          <el-input v-model="studentForm.age" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="学生住址" :label-width="formLabelWidth">
          <el-input v-model="studentForm.addr" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="所在班级" :label-width="formLabelWidth">
          <el-select v-model="studentForm.cid" placeholder="请选择所在班级">
            <el-option label="所有班级" value="0"></el-option>
            <el-option :label="c.cname" :value="c.cid" v-for="c in classes" :key="c.cid"></el-option>
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="save">确 定</el-button>
      </div>
    </el-dialog>
  </el-card>
</template>

<script>
  import {findAll, findByPage,search,save,delStud} from "@/api/student"
  import {findClasses} from "@/api/classes"

  export default {
    data() {
      return {
        students: [],       //列表学生
        page:1,             //当前页
        pageSize:5,         //页大小
        total:100,          //总页数
        studentSearch:{},   //条件查询
        classes:[],         //所有班级
        dialogFormVisible:false,  //添加或修改对话框
        formLabelWidth: '100px',  //对话框里文本框的宽度
        studentForm:{},           //添加或修改学生集合
        multipleSelection: [],    //批量删除
        sids:[]            //批量删除的id集合
      }
    },
    created() {
      //1. 查询所有学生
      // this.findAll()
      //2. 分页查询
      // this.findByPage()
      //3. 查询所有班级
      this.findClasses()
      //4. 条件查询带分页
      this.search()
    },

    methods: {
      //1. 列表所有学生
      findAll(){
        findAll().then(({data})=>{
          this.students = data.list
        })
      },
      //2. 分页查询
      findByPage(){
        findByPage(this.page,this.pageSize).then(({data})=>{
          //2.1 更新总记录数
          this.total = data.pr.total
          //2.2 更新当前页记录数
          this.students = data.pr.rows
        })
      },
      //3. 查询所有班级
      findClasses(){
        findClasses().then(({data})=>{
          this.classes = data.list
        })
      },
      //4. 条件查询带分页
      search(){
        search(this.page,this.pageSize,this.studentSearch).then(({data})=>{
          //4.1 更新总记录数
          this.total = data.pr.total
          //4.2 更新当前页记录数
          this.students = data.pr.rows
        })
      },
      //4. 添加界面
      addUI(){
        //4.1 清空对话框
        this.studentForm = {}
        //4.2 打开对话框
        this.dialogFormVisible = true

      },
      //5. 保存学生
      save(){
        let url = 'student/add'
        //5.1 判断sid是否有值，然后决定做添加还是修改操作
        if (this.studentForm.sid){
          url = 'student/update'
        }
        save(url,this.studentForm).then((data)=>{
          const _this = this
          if (data.code == 20000){
            this.$message({
              message: data.message,
              type: 'success',
              onClose(){
                //①关闭对话框
                _this.dialogFormVisible = false
                //②重新刷新页面
               this.search()
              }
            });
          }
        })
      },

      //每页大小(点击记录数时触发例如5条、10条每页)
      handleSizeChange(val) {
        this.pageSize = val
        // this.findByPage()
        this.search()
        console.log(`每页 ${val} 条`);

      },
      //当前页数（点击分页的第几页时触发）
      handleCurrentChange(val) {
        this.page = val
        this.search()
        // this.findByPage()
        console.log(`当前页: ${val}`);
      },
      //6. 修改学生
      handleEdit(index, row) {
        //6.1 为当前表单赋值
        this.studentForm = row
        //6.2 打开对话框
        this.dialogFormVisible = true
        console.log(index, row);
      },
      //8. 批量删除时选择某一行时触发
      handleSelectionChange(val) {
        this.multipleSelection = val;
      },
      //9. 删除学生
      handleDelete(index, row) {
        //9.1 处理批删除与单个删除的id，放到一个统一的sids
        if (row){
          this.sids = [row.sid]
        }else {
          this.sids = this.multipleSelection.map(m => m.sid);
        }
        //确认删除？
        this.$confirm(`是否删除编号为${this.sids}的学生？`, '删除提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          delStud(this.sids).then((data)=>{
            const _this = this
            if (data.code == 20000){
              this.$message({
                type: 'success',
                message: data.message,
                onClose(){
                  //刷新页面
                  _this.search()
                }
              });
            }
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          });
        });
      }
    }
  }
</script>

<style scoped>
.el-pagination{
  margin-top: 15px;
}
</style>
